/*
 * history.c
 *
 *  Created on: Jan 27, 2012
 *      Author: mbs6597
 */
#include "history.h"
#include <stdlib.h>
#include <stdio.h>
History * makeHistory(int size) {
	int i;
	History * hist = (History *) (malloc(sizeof(History)));
	hist->input = (float *) (malloc(size * sizeof(float)));
	hist->output = (float *) (malloc(size * sizeof(float)));
	hist->size = size;
	hist->zero_index = 0;
	for (i = 0; i < size; i++) {
		hist->input[i] = 0;
		hist->output[i] = 0;
	}

	return hist;
}

void setInput(History* hist, int z, float value) {
	hist->input[((hist->zero_index + z) + hist->size) % hist->size] = value;
}
void setOutput(History* hist, int z, float value) {
	hist->output[((hist->zero_index + z) + hist->size) % hist->size] = value;
}
float getInput(History* hist, int z) {
	return hist->input[((hist->zero_index + z) + hist->size) % hist->size];
}
float getOutput(History* hist, int z) {
	return hist->output[((hist->zero_index + z) + hist->size) % hist->size];
}
void stepTime(History* hist) {
	hist->zero_index = (hist->zero_index + 1) % hist->size;
}

void printhist(History * hist) {
	int i;
	printf("\n\t");
	for(i = -hist->size+1;i<=0;i++) {
		printf("%d\t",i);
	}
	printf("\ninput\t");
	for(i = -hist->size+1;i<=0;i++) {
		printf("%.2f\t",getInput(hist,i));
	}
	printf("\noutput\t");
	for(i = -hist->size+1;i<=0;i++) {
		printf("%0.2f\t",getOutput(hist,i));
	}
	fflush(stdout);
}
